<template>
  <header>
    <div class="wrapper">
      
    </div>
  </header>
  <div class="common-layout">
    <el-container>
      <el-header style="margin-left: -20px;">
        <el-menu class="horizontal-menu" mode="horizontal" background-color="#545c64" text-color="#fff">
          <el-menu-item><span style="color: white;font-size: 18px;">欢迎 <span
                style="color:blue;font-size: 21px;">{{ store.ccstore.nickName }}</span>登录</span> </el-menu-item>
          <el-menu-item class="logout-menu-item">
            <div style="float: right;">
              <RouterLink to="/">退出登录</RouterLink>
            </div>
          </el-menu-item>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu active-text-color="#ffd04b" background-color="#545c64" text-color="#fff" @select="" router
            style="height: 85vh;" mode="vertical">
            <!-- 使用递归组件渲染菜单 -->
            <MenuTree :menus="APImenuTree" />
          </el-menu>
        </el-aside>
        <el-main>
          <RouterView />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from '../components/HelloWorld.vue'
import { useCounterStore } from '@/stores/counter';
import axios from 'axios';
import MenuTree from './module components/MenuTree.vue';

const store = useCounterStore();

//#region  导航栏

//#endregion


//#region  菜单栏 API提供树形结构数据
var APImenuTree = ref([])
const GetMenuTree = () => {
  axios.get(`https://localhost:7174/api/Menu/GetTreeMenu?UserId=${store.ccstore.userId}`).then(res => {
    APImenuTree.value = res.data.data
    // console.log(APImenuTree.value, "API获取数据存入APImenuTree.value中")
  })
}


//#endregion


//#region 菜单栏 需要前端转换

// 获取菜单的数据
const MenuData = ref([])
//获取菜单
const GetMenuData = () => {
  axios.get(`https://localhost:7174/api/RoleMenu/GetUserMenu?UserId=${store.ccstore.userId}`).then(res => {
    MenuData.value = res.data.data
    // console.log(MenuData.value, "获取菜单数据")
    //将扁平数据转换为树形结构方法
    buildMenuTree(res.data.data);
  })
}

//转换树形
function buildMenuTree(menuList: any) {
  // console.log(menuList, "传过来的原数据")
  // 去重：以 menuId 为键，确保每个菜单项唯一
  const menuMap: any = {};
  menuList.forEach((item: any) => {
    if (!menuMap[item.menuId]) {
      menuMap[item.menuId] = {
        menuId: item.menuId,
        menuName: item.menuName,
        url: item.url,
        parentId: item.parentId,
        children: []
      };
    }
  });
  const uniqueMenus = Object.values(menuMap);

  // 构建哈希表并初始化树
  const nodeMap = {};
  const tree: any = [];
  uniqueMenus.forEach((menu: any) => {
    nodeMap[menu.menuId] = { ...menu, children: [] };
  });

  // 建立父子关系
  uniqueMenus.forEach(menu => {
    const node = nodeMap[menu.menuId];
    const parentId = menu.parentId;
    if (parentId !== null && parentId !== undefined) {
      const parent = nodeMap[parentId];
      if (parent) {
        parent.children.push(node);
      } else {
        // 父节点不存在，作为根节点
        tree.push(node);
      }
    } else {
      // 无父节点，直接作为根节点
      tree.push(node);
    }
  });
  menuTree.value = tree
  // console.log(menuTree.value, "转换后的Tree菜单名称")
  return tree;
}

//存放转换完成的数据
const menuTree = ref();


//#endregion



onMounted(() => {
  //API直接获取菜单树形结构数据
  GetMenuTree();
  //获取菜单数据普通
  // GetMenuData();
})


</script>

<style scoped>
.el-menu-vertical-demo {
  width: 200px;
}

html,
body {
  height: 100%;
}

/* 设置 el-menu 为 flex 布局 */
.horizontal-menu {
  display: flex;
  align-items: center;
}

/* 将退出登录菜单项推到最右侧 */
.logout-menu-item {
  margin-left: 130vh;
}

/* header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
} */
</style>
